﻿using Microsoft.Win32;

namespace CMWVS.Core.Mono
{
    public class MonoConfigCreator
    {
        private static MonoConfig m_Config = null;

        public static MonoConfig Create()
        {
            if (null == m_Config)
            {
                m_Config = GetMonoVersionConfigCreator().Create();
            }

            return m_Config;
        }

        private static ICreateMonoConfig GetMonoVersionConfigCreator()
        {
            string monoregistryKey = @"SOFTWARE\Novell\Mono";

            string monoRootDir = string.Empty;

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(monoregistryKey, false))
            {
                if (null == key)
                {
                    return new CreateNoMonoInstalled();
                }

                string defaultMonoCLR = key.GetValue("DefaultCLR") as string;
                if (string.IsNullOrWhiteSpace(defaultMonoCLR))
                {
                    return new CreateNoMonoInstalled();
                }

                //Version monoVersion = new Version(defaultMonoCLR);
                RegistryKey monoClr = key.OpenSubKey(defaultMonoCLR);
                if (null == key)
                {
                    return new CreateNoMonoInstalled();
                }

                monoRootDir = monoClr.GetValue("SdkInstallRoot") as string;
                if (string.IsNullOrWhiteSpace(monoRootDir))
                {
                    return new CreateNoMonoInstalled();
                }
            }

            return new CreateMonoConfig(monoRootDir.Trim());
        }
    }
}